Skip to content

fix: add ESM exports to support consistent CJS interop in Vite/Rolldown#95

Closed
neisukee wants to merge 1 commit intocodler:masterfrom
neisukee:fix/esm-exports
Closed

fix: add ESM exports to support consistent CJS interop in Vite/Rolldown#95
neisukee wants to merge 1 commit intocodler:masterfrom
neisukee:fix/esm-exports

Conversation

@neisukee
Copy link

Problem

With the introduction of consistent CJS interop in Vite (powered by Rolldown),
projects using "type": "module" receive module.exports as the default import
instead of module.exports.default.

This causes react-ga4's default import to return the entire exports object
rather than the GA4 instance, breaking ga4.initialize() and other methods.

See: https://rolldown.rs/in-depth/bundling-cjs#ambiguous-default-import-from-cjs-modules

Solution

Add an ESM entry point via the exports field in package.json,
so bundlers can resolve the proper ESM version directly.

Changes

  • Modified babel.config.js to support ESM build (modules: false when BABEL_ENV=esm)
  • Added .js extensions to relative imports in src/ for Node.js ESM compatibility
  • CJS build output moved to dist/cjs/
  • ESM build output added at dist/esm/
  • Added exports, module fields to package.json

@JORDAAAN1495
Copy link

JORDAAAN1495 commented Mar 23, 2026

This would be really good to get merged as this issue prevents consumers upgrading to the latest Vite version

@codler
Copy link
Owner

codler commented Mar 24, 2026

Thank you! This has now been resolved in https://github.com/codler/react-ga4/releases/tag/v3.0.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants